#include "GameHScrollBar.h"
#include "MyRender.h"
#include "MyInputMgr.h"
extern CMyRender g_kRender;
extern CMyInputMgr g_kInputMgr;

CGameHScrollBar::CGameHScrollBar(void)
{
	m_eWndType = WT_H_SCROLL_BAR;
	m_iCurPos = 0;
	m_iMaxRange = 6;
	m_pkBtnLeft = m_pkBtnMid = m_pkBtnRight = NULL;
	m_bBtnMidDown = false;
	m_iOffsetX = 0;
	m_dwUpdateBarTime = 0;
}

CGameHScrollBar::~CGameHScrollBar(void)
{
}

void CGameHScrollBar::OnCreate()
{
	m_pkBtnLeft = new CGameButton();
	m_pkBtnLeft->Create("btLeft",0,0,1,1,this);

	m_pkBtnRight = new CGameButton();
	m_pkBtnRight->Create("btRight",0,0,1,1,this);

	m_pkBtnMid = new CGameButton();
	m_pkBtnMid->Create("btMid",0,0,1,1,this);
}

void CGameHScrollBar::Init( int iLeftW,int iRightW )
{
	if (iLeftW + iRightW >= m_iWidth)
	{
		assert(0);
		iLeftW = iRightW = int((m_iWidth - 4) / 2.0f);
	}

	m_pkBtnLeft->ReSize(iLeftW,m_iHeight);
	m_pkBtnRight->ReSize(iRightW,m_iHeight);
	m_pkBtnMid->ReSize(m_iWidth - iLeftW - iRightW,m_iHeight);

	m_pkBtnLeft->Move(0,0);
	m_pkBtnRight->Move(m_iWidth - iRightW,0);
	m_pkBtnMid->Move(iLeftW,0);

	UpdateBar();
	return;
}

void CGameHScrollBar::LoadResFromDir(const string& rkDir)
{
	m_pkBtnLeft->LoadResFromFile(rkDir + "/" + "Left.bmp");
	m_pkBtnRight->LoadResFromFile(rkDir + "/" + "Right.bmp");
	m_pkBtnMid->LoadResFromFile(rkDir + "/" + "Mid.bmp");

	return;
}

void CGameHScrollBar::LoadResFromFile( const string& rkFile )
{
	CMyImage kImage;
	kImage.Load(rkFile);

	int iW = kImage.GetWidth() / 3;
	int iH = kImage.GetHeight();

	CMyImage kImage2;
	kImage2.LoadFromImage(kImage,0,0,iW,iH);
	m_pkBtnLeft->LoadResFormImage(kImage2);

	kImage2.LoadFromImage(kImage,iW,0,iW,iH);
	m_pkBtnRight->LoadResFormImage(kImage2);

	kImage2.LoadFromImage(kImage,iW * 2,0,iW,iH);
	m_pkBtnMid->LoadResFormImage(kImage2);

	return;
}

bool CGameHScrollBar::OnLeftBtnDown( int iX,int iY )
{
	return true;
}

bool CGameHScrollBar::OnLeftBtnUp( int iX,int iY )
{
	return true;
}

bool CGameHScrollBar::OnMidBtnRoll( int iRollDelta,int iX,int iY )
{
	if (m_spkFocusControl != this)
	{
		return false;
	}

	int iStep = abs(iRollDelta);
	if (iRollDelta > 0)
	{
		GoLeft(iStep);
	}
	else
	{
		GoRight(iStep);
	}

	return true;
}

void CGameHScrollBar::OnUpdate()
{	
	int iW = m_iWidth - m_pkBtnLeft->GetWidth() - m_pkBtnRight->GetWidth();
	float fW = (float)iW / (float)m_iMaxRange;

	if (g_kInputMgr.IsLeftBtnDown())
	{
		if (m_bBtnMidDown)
		{
			int iAbsX = g_kInputMgr.GetMouseX() - m_iOffsetX;			
			int iX = iAbsX - m_iAbsX;		
			Region(iX,m_pkBtnLeft->GetWidth(),(int)(m_iWidth - m_pkBtnRight->GetWidth() - fW));
			m_pkBtnMid->Move(iX,0);

			int iPos = int((iX - m_pkBtnLeft->GetWidth()) / fW);
			Region(iPos,0,m_iMaxRange - 1);		

			if (iPos != m_iCurPos)
			{
				m_iCurPos = iPos;
				OnBarChange();
			}
		}
		else if (IsMouseOn() && g_kInputMgr.GetMouseX() < m_pkBtnMid->GetPosX())
		{
			if (m_dwUpdateBarTime == 0)
			{
				m_dwUpdateBarTime = GetTickCount() + INTERVAL;
			}

			if (GetTickCount() > m_dwUpdateBarTime)
			{
				GoLeft();
				m_dwUpdateBarTime = GetTickCount() + INTERVAL;
			}		
		}
		else if (IsMouseOn() && g_kInputMgr.GetMouseX() > m_pkBtnMid->GetPosX() + m_pkBtnMid->GetWidth())
		{
			if (m_dwUpdateBarTime == 0)
			{
				m_dwUpdateBarTime = GetTickCount() + INTERVAL;
			}

			if (GetTickCount() > m_dwUpdateBarTime)
			{
				GoRight();
				m_dwUpdateBarTime = GetTickCount() + INTERVAL;
			}
		}		
	}
	else
	{
		m_bBtnMidDown = false;
		m_iOffsetX = 0;
		m_dwUpdateBarTime = 0;
	}

	
// 	m_pkBtnMid->ReSize(m_iWidth,fH);
// 	m_pkBtnMid->Move(0,m_iCurPos * fH + m_pkBtnUp->GetHeight());

	return;
}

void CGameHScrollBar::OnSize()
{
	Init(m_pkBtnLeft->GetWidth(),m_pkBtnRight->GetWidth());
	m_bBtnMidDown = false;
	m_iOffsetX = 0;
	m_dwUpdateBarTime = 0;
	return;
}

bool CGameHScrollBar::OnChildMsg( ChildMsg eMsgType,const string& rkChildName,const string& rkUserData /*= ""*/ )
{
	if (eMsgType == CM_Click)
	{
		if (rkChildName == m_pkBtnLeft->GetName())
		{
			GoLeft();
			return true;
		}

		if (rkChildName == m_pkBtnRight->GetName())
		{
			GoRight();
			return true;
		}
	}
	else if (eMsgType == CM_LBtnDown)
	{
		if (rkChildName == "btMid")
		{
			if (m_iMaxRange > 1)
			{
				m_bBtnMidDown = true;
				m_iOffsetX = g_kInputMgr.GetMouseX() - GetChild(rkChildName)->GetPosX();
			}
			else
			{
				m_bBtnMidDown = false;
				m_iOffsetX = 0;
			}
		}
	}	

	return true;
}

void CGameHScrollBar::OnBarChange()
{
	if (m_pkParent)
	{
		m_pkParent->ProcChildMsg(CM_HSCrollBarChange,m_kName,m_kUserData);
	}

	return;
}

void CGameHScrollBar::SetRange( int iRange )
{
	if (iRange <= 1)
	{
		iRange = 1;
	}

	m_iMaxRange = iRange;
	UpdateBar();
}

int CGameHScrollBar::GetRange()
{
	return m_iMaxRange;
}

void CGameHScrollBar::SetPos( int iPos )
{
	if (iPos <= 0)
	{
		iPos = 0;
	}

	if (iPos >= m_iMaxRange)
	{
		iPos = m_iMaxRange - 1;
	}

	if (m_iCurPos == iPos)
	{
		return;
	}

	m_iCurPos = iPos;
	UpdateBar();
	OnBarChange();
}

int CGameHScrollBar::GetPos()
{
	return m_iCurPos;
}

void CGameHScrollBar::UpdateBar()
{
	int iW = m_iWidth - m_pkBtnLeft->GetWidth() - m_pkBtnRight->GetWidth();
	float fW = (float)iW / (float)m_iMaxRange;	
	m_pkBtnMid->ReSize((int)fW,m_iHeight);
	m_pkBtnMid->Move(int(m_iCurPos * fW + m_pkBtnLeft->GetWidth()),0);
}

void CGameHScrollBar::OnDraw()
{
	int iW = m_iWidth - m_pkBtnLeft->GetWidth() - m_pkBtnRight->GetWidth();
	float fW = (float)iW / (float)m_iMaxRange;

	for (int i = 0;i < m_iMaxRange;i++)
	{
		g_kRender.DrawFrame(int(i * fW + m_pkBtnLeft->GetPosX() + m_pkBtnLeft->GetWidth()),m_iAbsY,(int)fW,m_iHeight,0xFF00FFFF);
	}
}

void CGameHScrollBar::GoLeft(DWORD dwStep/* = 1*/)
{
	m_iCurPos -= dwStep;
	if (m_iCurPos <= 0)
	{
		m_iCurPos = 0;
	}

	UpdateBar();
	OnBarChange();
}

void CGameHScrollBar::GoRight(DWORD dwStep/* = 1*/)
{
	m_iCurPos += dwStep;
	if (m_iCurPos >= m_iMaxRange)
	{
		m_iCurPos = m_iMaxRange - 1;
	}

	UpdateBar();
	OnBarChange();
}

void CGameHScrollBar::GoToMaxPos()
{
	SetPos(m_iMaxRange - 1);
	return;
}